﻿Imports ATS.EncryptDecryptKeys
Class MainWindow
    Private kstrRegSubKeyName As String = "GENCPROS\\ATS2.1" 'The name for the sub-key to store registry info

    Private Structure CurrentDate
        Dim Day As String
        Dim Month As String
        Dim Year As String
    End Structure

    Private Sub btnRegisterLicenseKey_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnRegisterLicenseKey.Click
        Dim licenseContent As String() = CommonMethods.Decrypt(TextBoxPassPhrase.Text, TextBoxLicenseKey.Text).Split("|")
        Dim dateExpiration As Date = Nothing
        Dim strProductKey As String = String.Empty
        If licenseContent.Length > 1 Then
            strProductKey = licenseContent(0)
            dateExpiration = licenseContent(1)

        End If
        Dim strPassPhrase As String = TextBoxPassPhrase.Text
        Dim strUserName As String = CommonMethods.Encrypt(strPassPhrase, TextBoxCompanyName.Text.ToUpper)
        strUserName = strUserName.Remove(16, (strUserName.Length - 16))
        strProductKey = strProductKey.Replace("-", "")
        If strUserName = strProductKey Then
            AuthorizeComputer(strPassPhrase, TextBoxCompanyName.Text.ToUpper, dateExpiration)
        Else
            MessageBox.Show("The company name does not match the Product/License Key. Please contact GENCPROS")
        End If
    End Sub


#Region " - Common Methods - "


    'Private Function Decrypt(ByRef pPassPhrase As String, ByVal pHexStream As String) As String
    '    Try
    '        'pHexStream = pHexStream.Replace("-", "")
    '        Dim enc As System.Text.Encoding = System.Text.Encoding.ASCII
    '        Dim myByteArray As Byte() = Convert.FromBase64String(pHexStream)
    '        pHexStream = enc.GetString(myByteArray)

    '        Dim objSym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
    '        Dim encryptedData As New Encryption.Data
    '        encryptedData.Hex = pHexStream
    '        Dim decryptedData As Encryption.Data
    '        decryptedData = objSym.Decrypt(encryptedData, New Encryption.Data(pPassPhrase))
    '        Return decryptedData.Text
    '    Catch
    '        Return Nothing
    '    End Try
    'End Function

    'Private Function Encrypt(ByRef pPassPhrase As String, ByVal pTextToEncrypt As String) As String
    '    If pPassPhrase.Length > 16 Then
    '        'limitation of the encryption mechanism
    '        pPassPhrase = pPassPhrase.Substring(0, 16)
    '    End If

    '    If pTextToEncrypt.Trim.Length = 0 Then
    '        'the Text to encrypt not set!!!
    '        Return String.Empty
    '    End If

    '    Dim skey As New Encryption.Data(pPassPhrase)
    '    Dim sym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
    '    Dim objEncryptedData As Encryption.Data
    '    objEncryptedData = sym.Encrypt(New Encryption.Data(pTextToEncrypt), skey)
    '    Return objEncryptedData.ToHex
    'End Function
#End Region ' - Common Methods -

#Region " - Validate - "
    Private Function AuthorizeComputer(ByVal pPassPhrase As String, ByVal pRegCode As String, expireDate As Date) As Boolean
        Try
            'Dim strMotherboardID As String = Encrypt(pPassPhrase, ATS.EncryptDecryptKeys.CheckHardware.GetMotherBoardID.Trim)
            CreateRegKeys(pPassPhrase, pRegCode, expireDate)
            MessageBox.Show("ATS 2.1 license is now saved.", "ATS Licensing", MessageBoxButton.OK, MessageBoxImage.Information)
            Return True
        Catch ex As Exception
            MessageBox.Show("Failed to save ATS 2.1 license info", "ATS Licensing", MessageBoxButton.OK, MessageBoxImage.Information)
            Return False
        End Try
    End Function
#End Region ' - Validate -

#Region " - Create Registry Keys - "
    Private Sub CreateRegKeys(ByVal pPassPhrase As String, ByVal pRegCode As String, expireDate As Date)
        Try
            Dim oReg As Microsoft.Win32.RegistryKey
            oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
            oReg = oReg.CreateSubKey(kstrRegSubKeyName)
            Dim Current As CurrentDate
            If Not expireDate = Nothing Then
                Current.Day = expireDate.Day.ToString
                Current.Month = expireDate.Month.ToString
                Current.Year = expireDate.Year.ToString
                'If EncryptKeys = True Then
                Current.Day = CommonMethods.Encrypt(pPassPhrase, Current.Day)
                Current.Month = CommonMethods.Encrypt(pPassPhrase, Current.Month)
                Current.Year = CommonMethods.Encrypt(pPassPhrase, Current.Year)
                'End If
                oReg.SetValue("ActiveConnectedKeys", Current.Day)
                oReg.SetValue("ActiveConnectedModeKeys", Current.Month)
                oReg.SetValue("ActiveUniqueKeys", Current.Year)
                oReg.SetValue("LicenseKey", TextBoxLicenseKey.Text)
                oReg.SetValue("ProductKey", TextBoxProductKey.Text)
            End If
            oReg.SetValue("ConnectedCommonKeys", CommonMethods.Encrypt(pPassPhrase, pRegCode))
            oReg.SetValue("Enabled", "")
            ''----Store these two values, the program will check for matches on each run.
            ''----Just to be safe. :P
            'oReg.SetValue("CompID", strMotherboardID)
            oReg.Close()
        Catch
        End Try
    End Sub
#End Region ' - Create Registry Keys - 
End Class
